package com.xinzhidi.web.city.controller;

import com.google.gson.Gson;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import java.lang.reflect.Method;
import java.util.*;


public class TemplateExportUtil {


    //最大作用行
    private static int MAX_ROW = 65534;






    /**
     * 设置有效性
     *
     * @param offset 主影响单元格所在列，即此单元格由哪个单元格影响联动
     * @param sheet
     * @param rowNum 行数
     * @param colNum 列数
     */
    public static void setDataValidation(String offset, HSSFSheet sheet, int rowNum, int colNum) {
        DVConstraint formula = DVConstraint.createFormulaListConstraint("INDIRECT($" + offset + "$" + rowNum + ")");
        int firstRow = rowNum - 1;
        int lastRow = rowNum - 1;
        int firstCol = colNum - 1;
        int lastCol = colNum - 1;
        CellRangeAddressList rangeAddressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
        DataValidation cacse = new HSSFDataValidation(rangeAddressList, formula);
        cacse.createErrorBox("错误提示", "请使用下拉方式选择合适的值！");
        cacse.createPromptBox("下拉选择提示", "请使用下拉方式选择合适的值！");
        sheet.addValidationData(cacse);
    }


    /**
     * 数字下标转列
     *
     * @param index
     * @return     
     */
    public static String NumToExcel(int index) {
        int shang = 0;
        int yu = 0;
        List<Integer> list = new ArrayList<Integer>();   //10进制转26进制 倒序
        while (true) {
            shang = index / 26;
            yu = index % 26;
            index = shang;
            list.add(yu);
            if (shang == 0)
                break;
        }
        StringBuilder sb = new StringBuilder();
        for (int j = list.size() - 1; j >= 0; j--) {
            sb.append((char) (list.get(j) + 'A' - (j > 1 ? 1 : j)));     //倒序拼接  序号转字符 非末位 序号减去 1
        }
        return sb.toString();
    }




    /**
     * 根据反射获取list<String>
     *
     * @param path   路径
     * @param method 方法名称
     * @param queryMap  参数
     * @return returnVal（通过反射后获取到的list）
     */
    public static List<String> getListStrByClazz(String path, String method, String queryMap) {
        List<String> list = new ArrayList<>();
        Map<String,Object> paramMap = new HashMap<>();
        if(queryMap!=null && queryMap!=""){
            Gson gson = new Gson();
            paramMap = gson.fromJson(queryMap, paramMap.getClass());
        }
        Class enumClass = null;
        try {
            enumClass = Class.forName(path);
            if(queryMap!=null && queryMap!=""){
                Method printMethod = enumClass.getMethod(method, List.class);
                list = (List<String>) printMethod.invoke(enumClass.newInstance(), paramMap);
            }else{
                Method printMethod = enumClass.getMethod(method);
                list = (List<String>) printMethod.invoke(enumClass.newInstance());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据反射获取list<String>
     *
     * @param path   路径
     * @param method 方法名称
     * @param queryMap  参数
     * @return returnVal（通过反射后获取到的list）
     */
    public static Map<String, List<String>> getMapListStrByClazz(String path, String method, String queryMap) {
        Map<String, List<String>> Map = new HashMap<>();
        Map<String,Object> paramMap = new HashMap<>();
        if(queryMap!=null && queryMap!=""){
            Gson gson = new Gson();
            paramMap = gson.fromJson(queryMap, paramMap.getClass());
        }
        Class enumClass = null;
        try {
            enumClass = Class.forName(path);
            if(queryMap!=null && queryMap!=""){
                Method printMethod = enumClass.getMethod(method, List.class);
                Map = (Map<String, List<String>>) printMethod.invoke(enumClass.newInstance(), paramMap);
            }else{
                Method printMethod = enumClass.getMethod(method);
                Map = (Map<String, List<String>>) printMethod.invoke(enumClass.newInstance());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Map;
    }



}
